﻿using System;
using System.Linq;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Services.Helpers;
using Nop.Services.WindAlert;
using Nop.Web.Models.WindAlert;

namespace Nop.Web.Controllers
{
    public partial class WDataController : BasePublicController
    {
        #region Fields
        private readonly IWorkContext _workContext;
        private readonly IForecastDataService  _forecastDataService;
        private readonly IWaveDataService _waveDataService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly ISpotService _spotService;
        private readonly IWindHelpers _windHelper;

        #endregion

        #region Constructors

        public WDataController(IWorkContext workContext, IForecastDataService forecastDataService
            , IWaveDataService waveDataService, IDateTimeHelper dateTimeHelper
            , ISpotService SpotService, IWindHelpers windHelper)
        {
            this._workContext = workContext;
            this._forecastDataService = forecastDataService;
            this._waveDataService = waveDataService;
            this._dateTimeHelper = dateTimeHelper;
            this._spotService = SpotService;
            this._windHelper = windHelper;
        }

        #endregion

        #region Utilities

        [NonAction]
        protected bool IsCurrentUserRegistered()
        {
            return _workContext.CurrentCustomer.IsRegistered();
        }

        #endregion

        public ActionResult WaveTable()
        {
            var spots = _spotService.GetAllSpots(isWave:true);
            if (spots == null)
                return null;
            var model = new WaveDataTableModel();
            //get user current date
            model.CurrentUserDateTime = _dateTimeHelper.ConvertToUserTime(DateTime.Now);
            foreach (var spot in spots)
            {
                var waveSpotData = _waveDataService.GetWaveDataBySpotIdTop1(spot.Id);
                if (waveSpotData != null)
                {
                    var waveDataRow = new WaveDataRowModel()
                    {
                        SpotId = spot.Id, 
                        SpotName = spot.Name,
                        WaveAdjustedTime  = waveSpotData.WaveAdjustedTime, 
                        Hmax = waveSpotData.Hmax, 
                        Hs = waveSpotData.Hs, 
                        Direction = waveSpotData.Direction, 
                        tav = waveSpotData.Tav,
                        Temperture = waveSpotData.Temperture,
                        DirectionLetters = _windHelper.getDirectionNumToLetters(waveSpotData.Direction),
                    };
                    model.TableRows.Add(waveDataRow);
                }
            }
            return PartialView(model);
        }

        public ActionResult WaveForecastTable()
        {
            var spots = _spotService.GetAllSpots(isForecast: true);
            if (spots == null)
                return null;
            var model = new ForecastDataTableModel();
            //get user current date
            model.CurrentUserDateTime = _dateTimeHelper.ConvertToUserTime(DateTime.Now);
            foreach (var spot in spots)
            {
                DateTime test = DateTime.Now;
                var forecastSpotData = _forecastDataService.GetForecastDataBySpotId(spot.Id)
                                        .Where(fd=> fd.ForecastTime  > test  ).FirstOrDefault();
                if (forecastSpotData != null)
                {
                    var forecastDataRow = new ForecastDataRowModel()
                    {
                        SpotId = spot.Id,
                        SpotName = spot.Name,
                        ForecastTime = forecastSpotData.ForecastTime,
                        Hieght = forecastSpotData.Height,
                        Period = forecastSpotData.Period,
                        WaveDirection = forecastSpotData.WaveDirection,
                        WindDirection = forecastSpotData.WindDirection,
                        WindSpeed = forecastSpotData.WindSpeed,
                        WindDirectionLetters = _windHelper.getDirectionNumToLetters(forecastSpotData.WindDirection),
                    };
                    model.TableRows.Add(forecastDataRow);
                }
            }
            return PartialView(model);
        }

    }
}
